﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace omaura_oled
{    
    /// <summary>
    /// Class access the Script.txt for display script
    /// </summary>
    public class ScriptReader
    {
        #region "Parameters"
        //-- parent
        private OmauraOled OledInterface;
        private string pathToScript = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\omaura\\omaura oled\\Script.txt";
        
        //-- time current display
        public string TimeCurrent = "";
        //-- call session
        public string CallID = "";
        //-- dvd
        public string DVD = "";
        public string DVDTime = "";
        public bool DVDTimeTotal;
        //-- video
        public string Video = "";
        public string VideoTime = "";
        public bool VideoTimeTotal;
        //-- audio cd
        public string AudioCD = "";
        public string AudioCDTime = "";
        public bool AudioCDTimeTotal;
        //-- audio music
        public string AudioMusic = "";
        public string AudioMusicTime = "";
        public bool AudioMusicTimeTotal;
        //-- tv live
        public string TV = "";
        //-- tv recorded
        public string TVRecorded = "";
        public string TVRecordedTime = "";
        public bool TVRecordedTimeTotal;
        //-- picture
        public string Pictures = "";
        //-- radio
        public string Radio = "";
        //--
        #endregion "Parameters"

        #region "Constructors"
        //--
        public ScriptReader(OmauraOled parentOled)
        {
            OledInterface = parentOled;

            ReadScriptFile();
        }
        //--
        #endregion "Constructors"

        #region "Methods"
        //--
        public void ReadScriptFile()
        {
            //--- time display
            TimeCurrent = ReadScriptWithParameter("TimeCu");

            //--- call display
            CallID = ReadScriptWithParameter("CallID");

            //--- dvd display
            DVD = ReadScriptWithParameter("DVDPla");
            DVDTime = ReadScriptWithParameter("DVDTim");
            if (ReadScriptWithParameter("DVDTTo") == "1")
                DVDTimeTotal = true;
            else
                DVDTimeTotal = false;

            //--- video display
            Video = ReadScriptWithParameter("Videos");
            VideoTime = ReadScriptWithParameter("VideoT");
            if (ReadScriptWithParameter("VideTo") == "1")
                VideoTimeTotal = true;
            else
                VideoTimeTotal = false;

            //--- audio cd display
            AudioCD = ReadScriptWithParameter("AudioC");
            AudioCDTime = ReadScriptWithParameter("AuCDTi");
            if (ReadScriptWithParameter("AuCDTo") == "1")
                AudioCDTimeTotal = true;
            else
                AudioCDTimeTotal = false;

            //--- audio music display
            AudioMusic = ReadScriptWithParameter("AudioM");
            AudioMusicTime = ReadScriptWithParameter("AuMuTi");
            if (ReadScriptWithParameter("AuMuTo") == "1")
                AudioMusicTimeTotal = true;
            else
                AudioMusicTimeTotal = false;

            //--- television display
            TV = ReadScriptWithParameter("TVLive");

            //--- television recorded display
            TVRecorded = ReadScriptWithParameter("TVReco");
            TVRecordedTime = ReadScriptWithParameter("TVRecT");
            if (ReadScriptWithParameter("TVReTo") == "1")
                TVRecordedTimeTotal = true;
            else
                TVRecordedTimeTotal = false;

            //--- picture display
            Pictures = ReadScriptWithParameter("Pictur");

            //--- radio display
            Radio = ReadScriptWithParameter("Radio ");
        }
        public string ReadScriptWithParameter(string Parameter)
        {
            int i = 0;
            string firstPara;
            string line;
            string parameterData = "";
            bool found = false;

            using (StreamReader fileReader = new StreamReader(pathToScript, Encoding.Default))
            {
                while (((line = fileReader.ReadLine()) != null) && i < 255 && !found)
                {
                    i++;

                    firstPara = line[0].ToString();
                    for (int charI = 1; charI < 6; charI++)
                        firstPara += line[charI].ToString();

                    if (firstPara.Equals(Parameter))
                    {
                        found = true;

                        // Read the data
                        if (line[9] == '|')
                        {
                            int iM = 10;

                            while (line[iM] != '|')
                            {
                                parameterData += line[iM];
                                iM++;
                            }
                        }
                    }
                }
            }
            if (!found)
                Log.Write("Error trying to access parameter: " + Parameter);
            //-- return
            return (parameterData);
        }
        //--
        public void CopyScriptFrom(string themeName)
        {
            // Delete old config file
            File.Delete(pathToScript);
            // copy the config file from the iconPath one
            File.Copy(".//Resources//Themes//" + themeName + "//Script.txt", pathToScript);
        }
        //--
        #endregion "Methods"
    }
}